from app.config import config
from app.include.mqttclient import Mqttclient
from app.include.setnetwork import Setnetwork
from app.include.zmkeys import Zmkeys
from app.initnet import Initnet
from app.func import Func

class Statr():

    def __init__(self):
        self.config = config
        self.init = Initnet(self.config)
        self.setnet = Setnetwork(self.config)
        self.func = Func()
        
    def setup_net(self):
        # 长按按键开始配网
        Zmkeys(self.config['gpio']['setnet'], self.setnet.main).keyParaInit()

    def main(self):
        is_net, is_init = self.init.main()

        if is_net and is_init:
            print("设备已经初始化并成功接入网络")
            mymqtt = Mqttclient(self.config)
            mymqtt.receivecall = self.func.mqtt_receive
            mymqtt.main()
            self.func.mqtt_send = mymqtt.send
        elif is_init is False:
            print('设备还没初始化')
            self.setnet.main()
        elif is_net is False:
            print('设备网络断开')

        self.setup_net()
